package com.jinlejia.erp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;

import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ItemListDropDownBinding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ListMapDropDownAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Map<String, Object>> list;
    private int checkItemPosition = 0;

    public void setCheckItem(int position) {
        checkItemPosition = position;
        notifyDataSetChanged();
    }

    public ListMapDropDownAdapter(Context context, ArrayList<Map<String, Object>> list) {
        this.context = context;
        this.list = list;
    }

    public void setData( ArrayList<Map<String, Object>> list ){
        this.list = list;
        Map<String,Object> map = new HashMap<>();
        map.put("valueName","不限");
        this.list.add(0,map);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView != null) {
            viewHolder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list_drop_down, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }
        fillValue(position, viewHolder);
        return convertView;
    }

    private void fillValue(int position, ViewHolder viewHolder) {
        viewHolder.tvName.setText(list.get(position).get("valueName").toString());
        if (checkItemPosition != -1) {
            if (checkItemPosition == position) {
                viewHolder.tvName.setTextColor(context.getResources().getColor(R.color.yellow));
                viewHolder.ivFlag.setVisibility(View.VISIBLE);
            } else {
                viewHolder.tvName.setTextColor(context.getResources().getColor(R.color.textColor));
                viewHolder.ivFlag.setVisibility(View.GONE);
            }
        }
    }


    class ViewHolder {
        TextView tvName;
        ImageView ivFlag;

        ViewHolder(View view) {
            ItemListDropDownBinding binding =DataBindingUtil.bind(view);
            tvName = binding.tvName;
            ivFlag = binding.ivFlag;
        }
    }
}
